package com.example.maastock.ontime.quartz;


import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.maastock.entity.BookStockOnline;
import com.example.maastock.mapper.BookStockOnlineMapper;
import com.example.maastock.mapper.MessageDetailMapper;
import com.example.maastock.ontime.EmailService;
import com.example.maastock.ontime.OnlineController;
import com.example.maastock.ontime.SearchOnLine;
import lombok.extern.slf4j.Slf4j;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.quartz.TimeOfDay;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.AutowireCapableBeanFactory;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.net.PortUnreachableException;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@Component
@Slf4j
public class JobA implements Job {


    @Override
    public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {

        SearchOnLine searchOnLine = SpringContextUtil.getBean(SearchOnLine.class);
        EmailService emailService = SpringContextUtil.getBean(EmailService.class);
        log.info("searchOnline={}", searchOnLine);
        List<BookStockOnline> allList = searchOnLine.getAllList();

        List<BookStockOnline> getStockNumberDistList = allList.stream()
                .collect(Collectors
                        .toMap(BookStockOnline::getStockNumber, // 根据属性进行分组
                                myObject -> myObject, // 保留对象本身
                                (obj1, obj2)
                                        ->
                                        obj1.getCreateTime()
                                                .compareTo(obj2.getCreateTime()) > 0 ? obj1 : obj2)) // 根据createTIme获取最近的对象
                .values()
                .stream()
                .sorted(Comparator.comparing(BookStockOnline::getCreateTime).reversed()) // 根据createTIme降序排序
                .collect(Collectors.toList());
        log.info("find_all_list={}", getStockNumberDistList);
        getStockNumberDistList.forEach(a -> {
            Object o = searchOnLine.searchOnlineInTime(a.getStockNumber());
            //强转为
            BigDecimal nowPrice = new BigDecimal(o.toString());
            if (ObjectUtil.isNotNull(a.getBookPrice())) {
                Boolean allowToSendMessage = checkNowPrice(nowPrice, a.getBookPrice());
                log.info("stock_num={},now_price={},allow_to_send_message={}", a.getStockNumber(), nowPrice, allowToSendMessage);
                if (allowToSendMessage) {
                    String subject = "sell-high-stock-email :"+ a.getStockName();
                    String emailMessage = spellEmailMessage(a.getStockNumber(), nowPrice,a.getBookPrice());
//                    emailService.sendEmail(a.getEmailAddress(), subject, emailMessage, a.getStockNumber());
                }
            }
            if (ObjectUtil.isNotEmpty(a.getLowPrice())) {
                if (checkNowPriceAndLowPrice(nowPrice, a.getLowPrice())) {
                    String subject = "sell-low-price-stock-email";
                    String emailMessage = spellEmailMessage(a.getStockNumber(), nowPrice, a.getLowPrice());
//                    emailService.sendEmail(a.getEmailAddress(), subject, emailMessage, a.getStockNumber());
                }
            }


        });


        System.out.println("JobA>>>>>>" + new Date());

    }


    /**
     * 判断db-price和当前交易价格的区别
     */
    public Boolean checkNowPrice(BigDecimal nowPrice, BigDecimal dbPrice) {
        return nowPrice.compareTo(dbPrice) == 0 || nowPrice.compareTo(dbPrice) > 0;
    }


    public Boolean checkNowPriceAndLowPrice(BigDecimal nowPrice, BigDecimal dbLowPrice) {
        return nowPrice.compareTo(dbLowPrice) == 0 || nowPrice.compareTo(dbLowPrice) < 0;
    }


    public String spellEmailMessage(String stockNumber, BigDecimal nowPrice , BigDecimal settingBookPrice) {
        String text = "股票代码:" + stockNumber + "目前:" + nowPrice.toString() + "设定价格为："+settingBookPrice;
        return text;
    }


}
